<?php
/**
 * 我的课包
 * @author 鸣枫
 *
 */
error_reporting(E_ALL ^ E_NOTICE);
class Coursepackage extends CT_Controller{
    
    private $member_id;     //用户ID
    
    function __construct() {
        parent::__construct();
        
        $this->member_id = $this->_uid;
        
        if(!$this->member_id && !$this->input->is_ajax_request()){
            redirect('/login');
        }
    }
    
    const SIZE = 5;        //分页显示数量
    
    /**
     * 课包列表
     */
    public function cplist(){
        try {
            $this->load->helper("arraytool");   //加载工具
            $type = intval($this->input->get('type'));    //类型
            
            $this->load->model('Order_model');
            $fild = 'id as order_id,course_package_id,hours,surplus_hours,status';
            $where = " member_id = {$this->member_id} and nursery_id ={$this->_nursery_id} ";
            switch ($type){
                case 1:     //未用完
                    $where .= " AND surplus_hours != 0 ";
                    break;
                case 2:     //已用完
                    $where .= " AND surplus_hours = 0 ";
                    break;
            }
            $order_by = 'id desc';
            $order_list = $this->Order_model->findAll($where,$fild,$order_by);
            if(empty($order_list))throw new Exception('没有课包记录');
            
            //获取课包信息
            $this->load->model('Course_Package_model');
            $fild = 'id,title as book_title,logo as book_logo';
            $where = " id IN ( ". filterValue($order_list,'course_package_id',true) ." )";
            $course_package_list = $this->Course_Package_model->findAll($where,$fild);
            $course_package_list = restByKey($course_package_list,'id');
            $order_list = arrayMerge($order_list,'course_package_id',$course_package_list);   //合并
            
            $this->ct_template->write('_title', '我的课包');
            $this->ct_template->write_view('content', 'coursepackage/cplist', array(
                'order_list'    =>  $order_list,
                'type'  =>  $type,
            ), FALSE);
            $this->ct_template->render();
            
        }catch (Exception $e){
            $this->ct_template->write('_title', '错误提示');
            $this->ct_template->write_view('content', 'errors/html/warn', array('msg'=>$e->getMessage() ), FALSE);
            $this->ct_template->render();
        }
        
    }
    /**
     * 详情
     */
    public function info(){
        try {
            $order_id = intval($this->input->get('order_id'));    //订单ID
            if(empty($order_id))throw new Exception('订单ID为空');
            
            $this->load->helper("arraytool");   //加载工具

            $this->load->model('Order_model');
            $fild = 'status,reason,id,course_package_id,hours,surplus_hours,pay_way,pay_fee,pay_time';
            $where['member_id'] = $this->member_id;
            $where['id'] = $order_id;
            $order_info = $this->Order_model->find($where,$fild);
            if(empty($order_info))throw new Exception('没有订单记录');
                        
            //获取课包信息
            $this->load->model('Course_Package_model');
            $fild = 'id,title as book_title,logo as book_logo'; 
            $course_package_info = $this->Course_Package_model->find(array('id'=>$order_info['course_package_id']),$fild);
            if(empty($course_package_info))throw new Exception('没有课包记录');
            
            //获取约课记录
            $this->load->model('Book_model');
            $fild = 'id,book_member_id,teacher_id,status,book_date,book_date_start,book_date_end,hours,order_id';
            $where = array();
            $where['course_package_id'] = $order_info['course_package_id'];
            $where['member_id'] = $this->member_id;
            $where['order_id'] = $order_id;
            
            $book_list = $this->Book_model->findAll($where,$fild);
            if($book_list){
                //获取教师信息
                $this->load->model('Teacher_model');
                $fild = 'id,name as teacher_name,avatar as teacher_avatar';
                $where = " id IN ( " . filterValue($book_list,'teacher_id',true) . ")";
                $teacher_list = $this->Teacher_model->findAll($where,$fild);
                $teacher_list = restByKey($teacher_list,'id');
                $book_list = arrayMerge($book_list,'teacher_id',$teacher_list);   //合并
                
                //获取用户信息
                $this->load->model('Book_Member_model');
                $fild = 'id,book_id,address';
                $where = " book_id IN ( " . filterValue($book_list,'id',true) .")";
                $book_member_list = $this->Book_Member_model->findAll($where,$fild);
                $book_member_list = restByKey($book_member_list,'book_id');
                $book_list = arrayMerge($book_list,'id',$book_member_list);   //合并
            }
            $this->ct_template->write('_title', '课包详情');
            $this->ct_template->write_view('content', 'coursepackage/cpinfo', array(
                'order_info'   =>  $order_info,
                'course_package_info'  => $course_package_info,
                'book_list'    =>  $book_list,
                'book_status' => array('待上课','上课中','已下课','已结束'),
                'weekArr' => array("周日","周一","周二","周三","周四","周五","周六"),
            ), FALSE);
            $this->ct_template->render();
            
            
        }catch(Exception $e){
            $this->ct_template->write('_title', '错误提示');
            $this->ct_template->write_view('content', 'errors/html/warn', array('msg'=>$e->getMessage() ), FALSE);
            $this->ct_template->render();
        }
        
        
    }
}



















